<template>
  <div>
    <div class="container">
      <div class="title">{{title}}</div>
      <van-tag
        v-if="show"
        closeable
        size="medium"
        type="primary"
        @close="close"
      >
      {{value}}
      </van-tag>
      <div class="picker-placeholder">请选择</div>
      <van-field
        readonly
        clickable
        @click="showPicker = true"
      />
      <van-popup v-model="showPicker" round position="bottom">
        <van-picker
          show-toolbar
          :columns="columns"
          @cancel="showPicker = false"
          @confirm="onConfirm"
        />
      </van-popup>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    title: {
      type: String,
      default: '标题',
    },
    defaultValue: {
      type: Object,
      default: ()=> ({
        value: '空',
      }),
    },
    defaultColumns: {
      type: Array,
      default:()=> [],
    },
  },
  data() {
    return {
      show: true,
      value: this.defaultValue.value,
      showPicker: false,
      columns:this.defaultColumns,
    }
  },
  methods: {
    onConfirm(value) {
      value instanceof Array
        ? this.value = `${value[0]} / ${value[1]}`
        : this.value = value

      if (value instanceof Array && value.length === 4)
        this.value = `${value[0]} / ${value[1]} / ${value[2]} / ${value[3]}`
      this.showPicker = false
      this.$emit('saveValue', value)
    },
    close() {
      this.show = false
    },
  },
}
</script>
<style lang="scss" scoped>
.container {
  position: relative;
  width: 375px;
  height: 106px;
  margin-bottom: 10px;
  overflow: hidden;
  background: #ffffff;
  .title {
    position: absolute;
    top: 10px;
    left: 36px;
    position: absolute;
    font-size: 12px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #8c97b2;
  }
  .picker-placeholder {
    position: absolute;
    left: 36px;
    bottom: 14px;
    position: absolute;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #8C97B2;
  }
  .van-field {
    position: absolute;
    bottom: 0px;
    left: 0px;
    background: transparent;
  }
  .van-tag {
    position: absolute;
    top: 36px;
    left: 20px;
    background: #EEEFF3;
    border-radius: 5px;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #002170;
    padding: 8px 14px;
  }
  .van-icon-cross {
    &::before {
      content: '\F034';
      margin-left: 8px;
      color: #BAC2D6;
    }
  }
  ::v-deep .van-picker-column__item {
    color: #002170;
  }
}
</style>